<!-- 新闻主页 -->
<template>
  <div class="container">
    <div
      style="display: flex; justify-content: space-between; align-items: center"
    >
      <span class="news">新闻政策</span>
      <el-input
        v-model="searchKey"
        style="width: 240px"
        size="large"
        placeholder="搜索新闻政策"
        @keyup.enter="getNewsList"
      />
      <div>
        <el-button
          v-if="userStore.userInfo?.role == 0"
          type="primary"
          size="large"
          @click="goEdit"
          >发布新闻政策</el-button
        >
      </div>
    </div>
    <div class="workCard">
      <el-card
        v-for="item in newsList"
        :key="item.id"
        style="
          max-width: 30%;
          min-width: 30%;
          cursor: pointer;
          margin-bottom: 50px;
          margin-left: 25px;
          margin-right: 25px;
        "
        body-style="padding:0"
        @click="goDetail(item.id)"
      >
        <img :src="item.cover" style="width: 100%" />
        <div class="newsTitle">{{ item.title }}</div>
        <div class="newsText">
          {{ item.text }}
        </div>
        <div class="newsDetail">查看详情 →</div>
      </el-card>
    </div>
    <div style="margin-top: 20px; display: flex; justify-content: flex-end">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        v-model:current-page="page"
        :pageSize="pageSize"
        @current-change="pageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { reqGetNewsList } from "@/api/api";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores/userInfo";
/**用户仓库，拿当前用户信息 */
const userStore = useUserStore();
/**搜索关键词 */
const searchKey = ref("");
/**新闻列表 */
const newsList = ref([]);
/**路由，用于路由跳转 */
const router = useRouter();

const pageSize = ref(12);
const page = ref(1);
const total = ref(100);

/**获取新闻政策列表 */
const getNewsList = async () => {
  const res = await reqGetNewsList({
    page: page.value,
    pageSize: pageSize.value,
    keyword: searchKey.value,
  });
  if (res.code === 200) {
    newsList.value = res.data.records;
    total.value = res.data.totalRow;
  } else {
    ElMessage.error(res.msg);
  }
};

/**页面加载时获取新闻政策列表 */
onMounted(() => {
  getNewsList();
});

/**分页改变时获取新闻政策列表 */
const pageChange = () => {
  getNewsList();
};

/**前往编辑新闻政策 */
const goEdit = () => {
  router.push("/news/edit");
};
/**前往新闻政策详情 */
const goDetail = (id) => {
  router.push(`/news/detail/${id}`);
};
</script>

<style lang="scss" scoped>
.container {
  padding: 40px 50px;
  .news {
    font-weight: bold;
    font-size: 18px;
  }
  .workCard {
    margin-top: 44px;
    display: flex;
    // justify-content: space-between;
    flex-wrap: wrap;
  }
  .newsTitle {
    font-weight: bold;
    font-size: 20px;
    margin-top: 20px;
    padding: 0 20px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .newsText {
    color: #4b5563;
    margin-top: 20px;
    padding: 0 20px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .newsDetail {
    margin-top: 20px;
    padding: 0 20px;
    margin-bottom: 30px;
  }
}
</style>
